home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / misc / csh547src.lha / set.c < prev    next >
C/C++ Source or Header  |  1995-06-04  |  13KB  |  550 lines

  1.  
  2. /*
  3.  * SET.C
  4.  *
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  * Version 2.07M by Steve Drew 10-Sep-87
  8.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  9.  * Version 5.00L by Urban Mueller 17-Feb-91
  10.  *
  11.  */
  12.  
  13. #include "shell.h"
  14.  
  15. static char *prompt_string( char *str, char *t );
  16. static void set_sys_var( char *name, char *val );
  17.  
  18. #define MAXLEVELS (4+MAXSRC)
  19.  
  20. static ROOT _Mbase[MAXLEVELS], *Mbase[MAXLEVELS], *Topbase[MAXLEVELS];
  21.  
  22. void
  23. init_mbase(void)
  24. {
  25.     int  i;
  26.     char *c;
  27.  
  28.     for( i=0; i<MAXLEVELS; i++ )
  29.         Topbase[i]=Mbase[i] = &_Mbase[i];
  30.  
  31. #ifdef isalphanum
  32.     for( c=isalph+'0'; c<=isalph+'9'; *c++=1 ) ;
  33.     for( c=isalph+'A'; c<=isalph+'Z'; *c++=1 ) ;
  34.     for( c=isalph+'a'; c<=isalph+'z'; *c++=1 ) ;
  35.     isalph['_']=1;
  36. #endif
  37. }
  38.  
  39.  
  40.  
  41. void
  42. set_var( int level, char *name, char *str )
  43. {
  44.     char *val;
  45.  
  46.     if(!str) str="";
  47.     if(!(val=malloc(strlen(str)+1))) {
  48.         ierror(NULL,512);
  49.         return;
  50.     }
  51.     strcpy( val, str );
  52.  
  53.     set_var_mem( level, name, val );
  54. }
  55.  
  56.  
  57.  
  58. void
  59. set_var_mem( int level, char *name, char *str ) /* does not make a copy */
  60. {
  61.     NODE **first, *node;
  62.     ROOT *root, *nul=0;
  63.     int  local= level & LEVEL_LOCAL;
  64.     char *t, c;
  65.  
  66.     for( t=name; isalphanum(*t); t++ ) ;
  67.     c = *t; *t=0;
  68.  
  69.     level &=~ LEVEL_LOCAL;
  70.  
  71.     for( root= Mbase[level]; root; root= local? nul : root->parent ) {
  72.         first= & root->first[*name & MAXHASH-1];
  73.         for( node = *first; node; node=node->next ) {
  74.             if( !strcmp( node->name, name) ) {
  75.                 free( node->text );
  76.                 goto copy;
  77.             }
  78.         }
  79.     }
  80.  
  81.     if(!(node=malloc(sizeof(NODE) + strlen(name)))) {
  82.         ierror(NULL,512);
  83.         return;
  84.     }
  85.     node->next = *first;
  86.     *first=node;
  87.     strcpy( node->name, name );
  88.  
  89. copy:
  90.     node->text=str;
  91.     if( *name=='_' )
  92.         set_sys_var( name, node->text );
  93.  
  94.     *t=c;
  95. }
  96.  
  97. static char VarBuf[256];
  98.  
  99. void *
  100. get_var( int level, void *varname )
  101. {
  102.     NODE *node;
  103.     ROOT *root;
  104.     char *t, c, *res=NULL;
  105.     int  f = *(char *)varname & MAXHASH-1;
  106.  
  107.     for ( t= varname; (signed char)*t>0; t++ ) ;
  108.     c = *t; *t=0;
  109.  
  110.     for( root= Mbase[level]; root; root=root->parent )
  111.         for( node=root->first[f]; node; node=node->next )
  112.             if( !strcmp(node->name,varname) )
  113.                 { res=node->text; goto done; }
  114.  
  115.     /* AMK: OS20-GetVar replaces ARP-Getenv */
  116.     if(level==LEVEL_SET && *(char*)varname!='_' && GetVar(varname,VarBuf,256,GVF_BINARY_VAR)>=0L)
  117.         res=VarBuf;
  118.  
  119. done:
  120.     *t=c;
  121.     return res;
  122. }
  123.  
  124. void
  125. unset_level( int level )
  126. {
  127.     NODE *node;
  128.     int i;
  129.  
  130.     for( i=0; i<MAXHASH; i++ ) {
  131.         for( node=Mbase[level]->first[i]; node; node=node->next ) {
  132.             if( *node->name=='_' && level==LEVEL_SET ) {
  133.                 Mbase[level]->first[i]= node->next;
  134.                 set_sys_var( node->name, get_var( LEVEL_SET, node->name ));
  135.             }
  136.             Free ( node->text );
  137.             Free ( node );
  138.         }
  139.         Mbase[level]->first[i] = NULL;
  140.     }
  141. }
  142.  
  143. void
  144. unset_var( int level, char *name )
  145. {
  146.     ROOT *root;
  147.     NODE **first, *node, *prev;
  148.     char *t, c;
  149.     int  f = *name & MAXHASH-1;;
  150.  
  151.     for( t=name; isalphanum(*t); t++ ) ;
  152.     c = *t; *t=0;
  153.  
  154.     for( root= Mbase[level]; root; root=root->parent ) {
  155.         first= & root->first[f];
  156.         for( node = *first, prev=NULL; node; prev=node, node=node->next ) {
  157.             if( !strcmp( node->name, name) ) {
  158.                 if( prev ) prev->next=node->next; else *first=node->next;
  159.                 Free( node->text );
  160.                 Free( node );
  161.                 if( *name=='_' )
  162.                     set_sys_var( name, NULL );
  163.                 goto done;
  164.             }
  165.         }
  166.     }
  167. done:
  168.     *t=c;
  169. }
  170.  
  171.  
  172. void
  173. set_var_n( int level, char *name, char *str, int n )
  174. {
  175.     char c, len=strlen(str);
  176.  
  177.     if( n>len )
  178.         n=len;
  179.  
  180.     if( n>=0 ) {
  181.         c=str[n]; str[n]=0;
  182.         set_var( level, name, str );
  183.         str[n]=c;
  184.     } else
  185.         set_var( level, name, "" );
  186. }
  187.  
  188. int
  189. do_unset_var( char *str, int level )
  190. {
  191.     int i;
  192.  
  193.     for (i = 1; i < ac; ++i)
  194.         unset_var (level, av[i]);
  195.     return 0;
  196. }
  197.  
  198. /*
  199.  * print given string, print control chars quoted (e.g. '^L' as '^' 'L')
  200.  *
  201.  * 05/18/93 ch
  202.  */
  203. void printf_ctrl(char *s)
  204. {
  205.     int c,i=0;
  206.     char *p;
  207.     if (p = malloc(strlen(s)*2 + 1)) {
  208.         while(c=(*s++)) {
  209.             if(c<32) {
  210.                 p[i++] = '^';
  211.                 p[i++] = 'A'-1+c;
  212.             }
  213.             else
  214.                 p[i++] = c;
  215.         }
  216.         p[i] = 0;
  217.         puts(p);
  218.         free(p);
  219.     }
  220.     else
  221.         puts(s);
  222. }
  223.  
  224. int
  225. do_set_var( char *command, int level )
  226. {
  227.     ROOT *root;
  228.     NODE *node;
  229.     char *str, *val;
  230.     int  i=2, j= *av[0]=='a' ? ' ' : 0xA0;
  231.  
  232.     if( ac>2 ) {
  233.         if( *av[i]=='=' && !av[i][1] && ac>3) i++;
  234.         val= compile_av( av, *av[i] ? i : i+1, ac, j, 0 );
  235.         set_var_mem(level, av[1], val);
  236.     } else if( ac==2 ) {
  237.         if (str=get_var(level,av[1])) {
  238.             printf("%-10s ",av[1]);
  239.             printf_ctrl(str);
  240.             /*putchar('\n');*/
  241.         }
  242.     } else if( ac==1 ) {
  243.         if( level& LEVEL_LOCAL )
  244.             root= Mbase[ level&~LEVEL_LOCAL];
  245.         else
  246.             root= Topbase[ level ];
  247.         for( i=0; i<MAXHASH && !breakcheck(); i++ )
  248.             for( node=root->first[i]; node && !dobreak(); node=node->next ) {
  249.                 printf("%s%-10s ",o_lolite,node->name);
  250.                 printf_ctrl(node->text);
  251.                 /*putchar('\n');*/
  252.                 /*quickscroll();*/
  253.             }
  254.     }
  255.  
  256.     return 0;
  257. }
  258.  
  259.  
  260. extern char shellvers[];
  261. extern char shellctr[];
  262. extern long ExecTimer, ExecRC;
  263. #define MAX_PITEM 3
  264.  
  265. static char *
  266. prompt_string( char *str, char *t )
  267. {
  268.     struct DateStamp dss;
  269.     char *u,*dev,buf[10];
  270.     static char *mypath = NULL;
  271.     static int mypathlen = 0;
  272.  
  273. #ifdef MULTIUSER_SUPPORT
  274.     struct muUserInfo *user_info; /* LILJA: Added for multiuser-support */
  275. #endif
  276.  
  277.     DateStamp(&dss);
  278.  
  279.     if( !str ) {
  280.         *t=0;
  281.         return t;
  282.     }
  283.  
  284.     while (*str) {
  285.         if (*str!='%') {
  286.             *t++ = *str++;
  287.             continue;
  288.         }
  289.         str+=2;
  290.         switch( str[-1] ) {
  291.         case 'P':
  292.             if (u=get_var(LEVEL_SET, "_cwd")) {
  293.  
  294.                 /* allocate bigger buffer? */
  295.                 if (strlen(u)+1 > mypathlen) {
  296.                     if (mypath) free(mypath);
  297.                     mypathlen = strlen(u)+1;
  298.                     if (!(mypath = malloc(mypathlen)))
  299.                         mypathlen = 0;
  300.                 }
  301.  
  302.                 if (mypath) {
  303.                     strcpy(mypath,u);
  304.  
  305.                     /* find last ':' in path */
  306.                     if (u=strlast(mypath,':')) {
  307.                         int i,max_pitem=o_promptdep,cnt=0;
  308.  
  309.                         /* NOTICE: "u" points to ':', NOT to char after ':' */
  310.  
  311.                         /* is "_promptdep" valid? */
  312.                         if (o_promptdep < 1 || o_promptdep > 65535) {
  313.                             fprintf(stderr,"csh: 1 <= _promptdep <= 65535\n");
  314.                             max_pitem = MAX_PITEM;
  315.                         }
  316.  
  317.                         /* count occurences of '/' (add 1 for no. of path parts) */
  318.                         /* (notice, that "u" still holds the leading ':') */
  319.                         for (i=strlen(u)-1; i>0 && cnt<max_pitem; i--) {
  320.                             if(u[i]=='/') cnt++;
  321.                         }
  322.  
  323.                         /* too much path parts? */
  324.                         if (cnt>=max_pitem) {
  325.                             /* strip only if remaining path is > 3 chars */
  326.                             if (i>3) {
  327.                                 u[1] = u[2] = u[3] = '.';
  328.                                 strdel(u,4,i-3);
  329.                             }
  330.                         }
  331.                         t+=sprintf(t,"%s",mypath);
  332.                     }
  333.                     else
  334.                         t+=sprintf(t,"%s",mypath);
  335.                 }
  336.                 else
  337.                     t+=sprintf(t,"%s", u);
  338.             }
  339.             else
  340.                 t+=sprintf(t,"%s", "(no _cwd)");
  341.             break;
  342.         case 'p': t+=sprintf(t,"%s", get_var(LEVEL_SET, "_cwd"));  break;
  343.         case 'm': t+=sprintf(t,"%d", (AvailMem( 0 )+512)/1024);    break;
  344.         case 't': t+=sprintf(t,"%s", next_word(dates(&dss,0)));    break;
  345.         case 'c': t+=sprintf(t,"%s", o_hilite);                    break;
  346.         case 'v': t+=sprintf(t,"%s", shellvers );                  break;
  347.         case 'n': t+=sprintf(t,"%s", get_var(LEVEL_SET,"_clinumber"));break;
  348.         case 'h': t+=sprintf(t,"%d", H_num);                       break;
  349.         case 'd':    sprintf(t,"%s", dates(&dss,0));if(u=index(t,' '))t=u;break;
  350.         case 'f': t+=sprintf(t,"%s", oneinfo(get_var(LEVEL_SET,v_cwd),4));break;
  351.         /* AMK: OS20-GetVar replaces ARP-Getenv */
  352.         case 's': if (GetVar(shellctr,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR)<0L)
  353.                     u = NULL;
  354.                   else
  355.                     u = buf;
  356.                   t+=sprintf(t,"%s", u?buf:"?");break;
  357.         case 'V': if (u=get_var(LEVEL_SET, "_cwd")) {
  358.                 if (dev = strdup(u)) {
  359.                     if (u=strlast(dev,':')) {
  360.                         *u = '\0';
  361.                         t+=sprintf(t,"%s",dev);
  362.                     }
  363.                     free(dev);
  364.                 }
  365.               }
  366.               break;
  367.         case 'x': t+=sprintf(t,"%2d",ExecRC);                             break;
  368.         case 'r': t+=sprintf(t,"%d", (signed char)
  369.                                        Myprocess->pr_Task.tc_Node.ln_Pri);break;
  370.         case 'e': t+=sprintf(t,"%d:%02d.%02d",ExecTimer/6000,ExecTimer/100%60,
  371.                                              ExecTimer%100);              break;
  372. #ifdef MULTIUSER_SUPPORT
  373.         /* LILJA: Added for multiuser-support */
  374.         case 'U':
  375.             if (muBase) {
  376.                 if(user_info=muAllocUserInfo()) {
  377.                     user_info->uid=muGetTaskOwner(NULL)>>16;
  378.                     t+=sprintf(t,"%s",muGetUserInfo(user_info,muKeyType_uid)?user_info->UserID:"<unknown>");
  379.                     muFreeUserInfo(user_info);
  380.                 }
  381.                 else
  382.                     t+=sprintf(t,"<unknown>");
  383.             }
  384.             else
  385.                 t+=sprintf(t,"<unknown>");
  386.             break;
  387. #endif
  388.         default : *t++=str[-2]; *t++=str[-1]; break;
  389.         }
  390.     }
  391.     *t=0;
  392.     return t;
  393. }
  394.  
  395. void
  396. push_locals( ROOT *newroot )
  397. {
  398.     int i;
  399.     NODE **nodeptr;
  400.  
  401.     newroot->parent=Mbase[ LEVEL_SET ];
  402.     Mbase[ LEVEL_SET ]=newroot;
  403.  
  404.     nodeptr=newroot->first;
  405.     for( i=MAXHASH; i>0; --i )
  406.         *nodeptr++=NULL;
  407. }
  408.  
  409. void
  410. pop_locals( void )
  411. {
  412.     unset_level( LEVEL_SET );
  413.     Mbase[ LEVEL_SET ]=Mbase[ LEVEL_SET ]->parent;
  414. }
  415.  
  416. int
  417. do_local(void)
  418. {
  419.     int i;
  420.  
  421.     if( ac==1 )
  422.         do_set_var( "", LEVEL_SET | LEVEL_LOCAL);
  423.     else 
  424.         for( i=1; i<ac; i++ )
  425.             set_var( LEVEL_SET | LEVEL_LOCAL, av[i], "");
  426.     return 0;
  427. }
  428.  
  429.  
  430. char truetitle[200];
  431.  
  432. char *o_rback="rback", *o_complete="*";
  433. char o_hilite[24], o_lolite[8], *o_csh_qcd, o_nobreak;
  434. char o_minrows, o_scroll, o_nowindow, o_noraw, o_vt100, o_nofastscr;
  435. char o_bground, o_resident, o_pipe[16]="T:", o_datefmt, o_nomatch=0;
  436. char o_abbrev=5, o_insert=1, *o_every, o_cquote=0, o_mappath=0;
  437. long o_noreq, o_failat=20, o_timeout=GWB_TIMEOUT_LOCAL;
  438. long o_promptdep=MAX_PITEM;
  439.  
  440. extern char trueprompt[100];
  441.  
  442. #define ISVAR(x) ( !strcmp( name, x ) )
  443.  
  444. static void
  445. set_sys_var( char *name, char *val )
  446. {
  447.     char *put, col;
  448.  
  449.     /*
  450.      *  assumption is that "val" is not dynamic but a pointer
  451.      *  to an internally allocated string for that variable and
  452.      *  is valid until next change ...
  453.      */
  454.  
  455.     if     (ISVAR(v_debug    )) debug     = val!=NULL;
  456.     else if(ISVAR(v_nobreak  )) o_nobreak = val!=NULL;
  457.     else if(ISVAR(v_insert   )) o_insert  = val!=NULL;
  458.     else if(ISVAR(v_nomatch  )) o_nomatch = val!=NULL;
  459.     else if(ISVAR(v_mappath  )) o_mappath = val!=NULL;
  460.     else if(ISVAR(v_cquote   )) o_cquote  = val!=NULL;
  461.     else if(ISVAR(v_every    )) o_every   = val;
  462.     else if(ISVAR(v_failat   )) o_failat  = val ? atoi(val) : 20;
  463.     else if(ISVAR(v_timeout  )) o_timeout = val ? atoi(val) : 1;
  464.     else if(ISVAR(v_rback    )) o_rback   = val ? val : "rback";
  465.     else if(ISVAR(v_abbrev   )) o_abbrev  = val ? atoi(val) : 0;
  466.     else if(ISVAR(v_promptdep)) o_promptdep = val ? atoi(val) : -1;
  467.     else if(ISVAR(v_complete )) o_complete= val ? val : "*";
  468. #if 0
  469.     else if(ISVAR(v_abbrev   )) o_abbrev  = val?*val!='n':1;
  470. #endif
  471.     else if(ISVAR(v_datefmt  )) o_datefmt = val && !strcmp(val,"subst");
  472.     else if(ISVAR(v_qcd      )) o_csh_qcd = val;
  473.     else if(ISVAR(v_noreq    )) Myprocess->pr_WindowPtr = (o_noreq=(val?1:0))? (APTR) -1L : 0L/*Mywindow*/;
  474.     else if(ISVAR(v_hist     )) S_histlen = val ? atoi(val) : 0;
  475.     else if(ISVAR(v_titlebar )) update_sys_var( v_titlebar );
  476.     else if(ISVAR(v_pipe     )) appendslash(strcpy(o_pipe,val?val:"t:"));
  477.     else if(ISVAR(v_verbose  )) {
  478.         Verbose=0;
  479.         if( val ) {
  480.             if( index(val,'s' )) Verbose|= VERBOSE_SOURCE;
  481.             if( index(val,'a' )) Verbose|= VERBOSE_ALIAS;
  482.             if( index(val,'h' )) Verbose|= VERBOSE_HILITE;
  483.         }
  484.     } else if( ISVAR(v_hilite)) {
  485.         o_hilite[0]=o_lolite[0]=0;
  486.         if( !val )
  487.             val="";
  488.         put= o_hilite;
  489.         while( val && *val ) {
  490.             switch( *val++ ) {
  491.             case 'b': put+=sprintf( put, "\033[1m" ); break;
  492.             case 'i': put+=sprintf( put, "\033[3m" ); break;
  493.             case 'u': put+=sprintf( put, "\033[4m" ); break;
  494.             case 'r': put+=sprintf( put, "\033[7m" ); break;
  495.             case 'c': put+=strlen(put);
  496.                       if( *val>='0' && *val<='9' ) {
  497.                          col = *val++;
  498.                          if( *val==',' && val[1]>='0' && val[1]<='9' ) {
  499.                              put+=sprintf( put,"\033[3%cm\033[4%cm",col,val[1]);
  500.                              val+=2;
  501.                          } else
  502.                              put+=sprintf( put,"\033[3%cm",col );
  503.                       }
  504.                       break;
  505.             }
  506.         }
  507.         *put=0;
  508.         if( *o_hilite )
  509.             strcpy(o_lolite,"\033[m");
  510.         strcpy(prompt_string(put,trueprompt),o_lolite);
  511.     } else if( ISVAR( v_scroll )) {
  512.         o_scroll=0;
  513.         if( val ) {
  514.             o_scroll=atoi( val );
  515.             if( o_scroll<2 ) o_scroll=0;
  516.             if( o_scroll>8 ) o_scroll=8;
  517.         }
  518.     } else if( ISVAR(v_minrows)) {
  519.         o_minrows=34;
  520.         if( val ) {
  521.             o_minrows=atoi( val );
  522.             if( o_minrows<8 )   o_minrows=8;
  523.             if( o_minrows>100 ) o_minrows=100, o_scroll=0;
  524.         }
  525.     }
  526. }
  527.  
  528.  
  529. extern BOOL nowintitle;  /* defined in main.c */
  530.  
  531. void
  532. update_sys_var( char *name )
  533. {
  534.     char c=name[1], *str, buf[250];
  535.  
  536.     if( c==v_prompt[1] ) {
  537.         if( (str=get_var(LEVEL_SET,v_prompt) ) ==NULL) str="$ ";
  538.         strcpy(prompt_string(str,trueprompt),o_lolite);
  539.     }
  540.     if( c==v_titlebar[1] && !o_nowindow && Mywindow ) {
  541.         prompt_string( get_var(LEVEL_SET, v_titlebar), buf);
  542.         if (strcmp((char*)Mywindow->Title, buf)) {
  543.             strcpy(truetitle,buf);
  544.             if (!nowintitle)
  545.                 SetWindowTitles(Mywindow, truetitle, (char *)-1);
  546.         }
  547.     }
  548. }
  549.  
  550.